String.prototype.contains = function(it) { return this.indexOf(it) != -1; };


function ajax_load(url,id)
{
	var ajax = new AJAX_Handler();
    
	ajax.onreadystatechange(change_);
	ajax.send(url);	
    	
	function change_()
	{
		if(ajax.handler.readyState == 4 && ajax.handler.status == 200)
        {
            if(url=='contentprocessing')
            {
                var tempChat=document.getElementById('content').innerHTML;
                
                document.getElementById('content').innerHTML = tempChat+ajax.handler.responseText;
            }
		  else 
           	document.getElementById(id).innerHTML = ajax.handler.responseText;
       }
           
	}
}

function synchronous_ajax(url, passData) 
{
  passData=passData?passData:"";
  
  if (window.XMLHttpRequest) 
  {
    AJAX=new XMLHttpRequest();
  } 
  else 
  {
    AJAX=new ActiveXObject("Microsoft.XMLHTTP");
  }
  
  if (AJAX) 
  {
    AJAX.open("POST", url, false);
    AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    AJAX.send(passData);
    return AJAX.responseText;
  } 
  else 
  {
    return false;
  }
}

function logout_func()
{
    synchronous_ajax("sessionservlet","param=logout");
    window.location="/MyWebChat";    
}

function _load(value){
    
	if(value == 'content')
    {
		ajax_load('contentprocessing','content');
        
		var objDiv = document.getElementById("main");
       
	    if(scrolling==false)	
            objDiv.scrollTop = objDiv.scrollHeight;
     }
     else if(value == 'online')
		ajax_load('onlineusers','userlist');	
}

function _refresh(value){
    
	if(value == 'content')
		setTimeout("_load('content');_refresh('content');", 2000);	//3s
	else if(value == 'online')	
		setTimeout("_load('online');_refresh('online');", 10000);//10s
	
}

function addsmile(kitu)
{
	window.opener.document.chatform.uptext.value = opener.document.chatform.uptext.value + kitu;
}

function upstyle(element)
{
	if (element == 'b')
	{
		if (document.getElementById('upb').className == 'sbutton')
			document.getElementById('upb').className = 'sbutton_active';
		else
			document.getElementById('upb').className = 'sbutton';
		
	}
	else if (element == 'i')
	{
		if (document.getElementById('upi').className == 'sbutton')
			document.getElementById('upi').className = 'sbutton_active';
		else
			document.getElementById('upi').className = 'sbutton';
		
	}
	else if (element == 'u')
	{
		if (document.getElementById('upu').className == 'sbutton')
			document.getElementById('upu').className = 'sbutton_active';
		else
			document.getElementById('upu').className = 'sbutton';	
	}
}
function check_form()
{
	if(document.getElementById('uptext').value == "")
	{
		alert("Bạn chưa nhập nội dung chat");
		document.getElementById('uptext').focus();
        return;
	}
    
	if(document.getElementById('uptext').value.length>255)
	{
		alert("Nội dung không được dài quá 255 kí tự");
		document.getElementById('uptext') = document.getElementById('uptext').substring(0,255);
		document.getElementById('uptext').focus();
        return;
	}
	else
    {
        
		send_chat();
    }
}

function smiliepopup()
{
	window.open("smilesloading", "", "location=no,scrollbars=yes,width=500,height=500");
}

function send_chat()
{
    wasSubmitted = true;
    
	var text = document.getElementById('uptext').value;
	var upcolor = document.getElementById('upcolor').value.toLowerCase();
	var upfont = document.getElementById('upfont').value;
	var upb = (document.getElementById('upb').className == 'sbutton_active') ? 'B' : '';
	var upi = (document.getElementById('upi').className == 'sbutton_active') ? 'I' : '';
	var upu = (document.getElementById('upu').className == 'sbutton_active') ? 'U' : '';		
	var param = 'text='+text+'&upfont='+upfont+'&upcolor='+upcolor+'&upb='+upb+'&upi='+upi+'&upu='+upu;				
	var ajax = new AJAX_Handler();
    
	ajax.onreadystatechange(send_chat_done);
	ajax.send("contentprocessing",param);
	document.getElementById('uptext').value='';	
    document.getElementById('uptext').focus();	
    	
	function send_chat_done()
	{
		if(ajax.handler.readyState == 4 && ajax.handler.status == 200){
		    var tempChat=document.getElementById('content').innerHTML;
			document.getElementById('content').innerHTML = tempChat+ajax.handler.responseText;
            
            var objDiv = document.getElementById("main");
	        objDiv.scrollTop = objDiv.scrollHeight;
            
		}
	}
	
    
}

function makeString(aString)
{
    var text = "";
    for( var i=0; i < aString.length ; i++ )
        text += aString[(i+3)%aString.length];

    return text;
}
function reg_check()
{
	var user = document.getElementById('username');
	var pass = document.getElementById('pass');
	var repass = document.getElementById('repass');
	if(user.value == "" || user.value.length<4){
		alert('Tên đăng nhập ko được để trống và phải lớn hơn 4 ký tự');
		user.focus();
		return false;
	}
	if(pass.value == "" || pass.value.length<=4){
		alert('Mật khẩu ko được để trống và phải lớn hơn 4 ký tự');	
		pass.focus();
		return false;
	}
	if(pass.value!=repass.value){
		alert('Hai ô mật khẩu ko giống nhau');
		pass.focus();
		return false;
	}
	else
    {
        //create random string from password to protect password
        pass.value = makeString(pass.value);
		reg_submit(user.value,pass.value);
    }
			
}
function reg_submit(user,pass)
{	
	var param = 'act=signup&user='+user+'&pass='+pass;
	var ajax = new AJAX_Handler();
	ajax.onreadystatechange(send_reg_done);
	ajax.send("signservlet",param);	
	document.getElementById('main').innerHTML = 'Loading...';	
	function send_reg_done()
	{
		if(ajax.handler.readyState == 4 && ajax.handler.status == 200)
        {
			document.getElementById('main').innerHTML = ajax.handler.responseText;
		}
	}
}
function log_check()
{
	var user = document.getElementById('username').value;
	var pass = document.getElementById('pass').value;
    
    pass = makeString(pass);
	var param = 'act=signin&user='+user+'&pass='+pass;
	var ajax = new AJAX_Handler();
	ajax.onreadystatechange(send_log_done);
	ajax.send("signservlet",param);	
	document.getElementById('main').innerHTML = 'Loading...';	
	function send_log_done()
	{
		if(ajax.handler.readyState == 4 && ajax.handler.status == 200){
			document.getElementById('main').innerHTML = ajax.handler.responseText;
		}
	}
}


var AJAX_Handler=function()
{
	this.xmlHttp=false;
	try 
	{
	  this.xmlHttp=new XMLHttpRequest();
	}
	catch(e)
	{
		try 
		{
		  
		  //Old versions of Internet Explorer (IE5 and IE6) 
		  this.xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch(e)
		{
			try
			{
			    
                // // Old version of Firefox, Opera 8.0-, Safari  
				this.xmlHttp=new ActiveXObject('Msxml2.XMLHTTP');
			}
			catch(e)
			{
				alert('Your browser does not support AJAX');return;
			}
		}
	}
	this.onreadystatechange=function(updateFunc)
	{
		this.updateFunc=updateFunc;
	}
	this.send=function(url,param)
	{
    	 param=param?param:"";
		this.xmlHttp.onreadystatechange=this.updateFunc;this.xmlHttp.open("POST",url);
		this.xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		this.xmlHttp.setRequestHeader("Content-length",param.length);
		this.xmlHttp.setRequestHeader("Connection","close");
		this.xmlHttp.send(encodeURI(param));
		this.handler=this.xmlHttp;
	}
}